<template>
	<view class="container">
		<uni-popup :show="type === 'quxiao'" position="middle" mode="fixed" @hidePopup="togglePopup('')" style="position: relative;">
			<view class="uni-tip">
				<text class="uni-tip-content">确定删除吗？</text>
				<view class="uni-tip-group-button">
					<text class="uni-tip-button" @click="togglePopup('')" style="border-right: 1px solid #eee;">取消</text>
					<text class="uni-tip-button" @click="confirmOrder()">确定</text>
				</view>
			</view>
		</uni-popup>

		<view v-if="cartList.length==0" class="empty">
			<image src="/static/empty.png" mode="widthFix"></image>
			<view class="empty-tips">
				暂无商品，随便逛逛~
			</view>
		</view>
		<view v-else style="padding-bottom: 134upx;">
			<!-- 列表 -->
			<view class="cart-list">
				<view>
					<view v-for="(cartitem,cartindex) in cartList" :key="cartindex"  style="background-color: #fff;padding: 20upx 30upx 0;">
						<view style="padding-left: 80upx;position: relative;display: flex;">
							<label class="radio" @click="check('item',cartindex)" >
								<radio value="r1" color="#EC6104" :checked="cartitem.checked" ></radio>
							</label>
							<view class="image-wrapper">
								<image :src="cartitem.images" ></image>
							</view>
							<view style="flex:1;margin-left: 24upx;">
								<view style="font-size: 26upx;" class="goods-info">{{cartitem.title}}</view>
								<view style="display: flex;margin-top: 40upx;align-items: center;">
									<text style="color: #EC6104;font-size: 28upx;margin-right: 28upx;">￥{{cartitem.price}}</text>
									<uni-number-box class="step" :index="cartindex" :min="Number(cartitem.buy_min_number)" :max="Number(cartitem.buy_max_number)" :disabled="true" :value="Number(cartitem.stock)"  @eventChange="numberChange"></uni-number-box>
								</view>
								<view style="color: #666;font-size: 26upx;margin-top: 8upx;" v-if="Number(cartitem.integral>0)">({{cartitem.integral}}VS)</view>
								<view style="position: absolute;bottom: 48upx;right: 30upx;" @click="deleteCartItem(cartitem.id,cartindex)">
									<image src="../../static/delete.png" style="width: 28upx;height: 28upx;"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- <view v-for="(item, index) in cartList" :key="item.id">
					<view class="cart-item" :class="{'b-b': index!==cartList.length-1}">
						<view class="image-wrapper">
							<image :src="item.image" :class="[item.loaded]" mode="aspectFill" lazy-load @load="onImageLoad('cartList', index)"
							 @error="onImageError('cartList', index)"></image>
							<view class="yticon icon-xuanzhong2 checkbox" :class="{checked: item.checked}" @click="check('item', index)"></view>
						</view>
						<view class="item-right">
							<text class="clamp title">{{item.title}}</text>
							<text class="attr">{{item.attr_val}}</text>
							<text class="price">¥{{item.price}}</text>
							<uni-number-box class="step" :min="1" :max="item.stock" :value="item.number>item.stock?item.stock:item.number"
							 :isMax="item.number>=item.stock?true:false" :isMin="item.number===1" :index="index" @eventChange="numberChange"></uni-number-box>
						</view>
						<text class="del-btn yticon icon-fork" @click="deleteCartItem(index)"></text>
					</view>
				</view> -->
			</view>
			<!-- 底部菜单栏 -->
			<view class="action-section">
				<view style="display: flex;padding: 30upx;justify-content: space-between;align-items: center;width: 100%;">
					<label style="font-size: 28upx;" @click="check('all')">
						<radio value="r1" color="#EC6104" :checked="allChecked">全选</radio>
					</label>
					<view style="display: flex;align-items: center;">
						<view style="font-size: 28upx;color: #333;">合计: <text style="color: #EC6104;margin:0 8upx;">￥{{total}}</text>({{total_integral}}VS)</view>
						<button class="tuibtn" @click="createOrder">结算</button>
					</view>
				</view>

			</view>
			<!-- <view class="action-section">
				<view class="checkbox">
					<image :src="allChecked?'/static/selected.png':'/static/select.png'" mode="aspectFit" @click="check('all')"></image>
					<view class="clear-btn" :class="{show: allChecked}" @click="clearCart">
						清空
					</view>
				</view>
				<view class="total-box">
					<text class="price">¥{{total}}</text>
					<text class="coupon">
						已优惠
						<text>74.35</text>
						元
					</text>
				</view>
				<button type="primary" class="no-border confirm-btn" @click="createOrder">去结算</button>
			</view> -->
		</view>
	</view>
</template>

<script>
	// import {
	// 	mapState
	// } from 'vuex';
	import postAjax from '../../API/postAjax.js'
	import uniNumberBox from '@/components/uni-number-box.vue'
	import uniPopup from '@/components/uni-popup/uni-popup.vue'
	export default {
		components: {
			uniNumberBox,
			uniPopup
		},
		data() {
			return {
				total: 0, //总价格
				total_integral:0,// 总VS
				allChecked: false, //全选状态  true|false
				empty: false, //空白页现实  true|false
				cartList: [	],//购物车数据
				mytoken:'',//token
				type:'',
				deleteindex:0,//删除购物车的index值
				deleteId:0
			};
		},
		onShow(){
			let that = this
			that.mytoken = uni.getStorageSync('token')
			that.loadData()
		},
		onLoad() {
		},
		watch: {
			//显示空白页
			cartList(e) {
				let empty = e.length === 0 ? true : false;
				if (this.empty !== empty) {
					this.empty = empty;
				}
			}
		},
		// computed: {
		// 	...mapState(['hasLogin'])
		// },
		methods: {
			//请求数据
			async loadData() {
				let that = this
				let list = await that.get_cart_data();
				let cartList = list.map(item => {
					item.checked = false;
					return item;
				});
				that.cartList = cartList;
				that.calcTotal(); //计算总价
			},
			togglePopup(type) {
				this.type = type;
			},
			navToLogin() {
				uni.navigateTo({
					url: '/pages/public/login'
				})
			},
			//选中状态处理
			check(type, index) {
				if (type === 'item') {
					this.cartList[index].checked = !this.cartList[index].checked;
				} else {
					const checked = !this.allChecked
					const list = this.cartList;
					list.forEach(item => {
						item.checked = checked;
					})
					this.allChecked = checked;
				}
				this.calcTotal(type);
			},
			//请求接口的数据
			get_cart_data(){
				let that = this
				return new Promise((resolve, reject) => {
					postAjax('Cart/Index',{token:that.mytoken}, function(data) {
						console.log(data)
						resolve(data.data.data)
					})
				})
			},
			//数量
			numberChange(data) {
				this.cartList[data.index].stock = data.number;
				this.calcTotal();
				this.numberOprate(this.cartList[data.index].id,this.cartList[data.index].stock,this.cartList[data.index].goods_id)
			},
			//删除
			deleteCartItem(id,index) {
				this.type = 'quxiao'
				this.deleteindex = index
				this.deleteId = id
			
			},
			// 数据的加减
			numberOprate(id,stock,goods_id){//购物车id 数量 商品id
				console.log(id,stock,goods_id,this.mytoken)
				postAjax('cart/stock',{
					token:this.mytoken,
					id:id,
					stock:stock,
					goods_id:goods_id
				},function(data){
					console.log(data)
				})
			},
			confirmOrder(){
				let that = this
				postAjax('cart/Delete',{
					token:that.mytoken,
					id:that.deleteId
				},function(data){
					if(data.code==0){
						uni.showToast({
						  title:'删除成功',
						  icon:'none'
						})
						that.cartList.splice(that.deleteindex,1)
						that.calcTotal()
					}else{
						uni.showToast({
						  title:'删除失败',
						  icon:'none'
						})
					}
					that.type=""
				})
				uni.hideLoading();
			},
			//清空
			clearCart() {
				uni.showModal({
					content: '清空购物车？',
					success: (e) => {
						if (e.confirm) {
							this.cartList = [];
						}
					}
				})
			},
			//计算总价
			calcTotal() {
				let list = this.cartList;
				if (list.length === 0) {
					this.empty = true;
					return;
				}
				let total = 0;
				let total_inte = 0
				let checked = true;
				list.forEach(item => {
					if (item.checked === true) {
						total += item.price * item.stock;
						total_inte += item.integral * item.stock
					} else if (checked === true) {
						checked = false;
					}
				})
				this.allChecked = checked;
				this.total = Number(total.toFixed(2))
				this.total_integral = Number(total_inte.toFixed(2))
			},
			//创建订单
			createOrder() {
				let that = this
				let list = that.cartList;
				if(that.total == 0){
					uni.showToast({
					  title:'请勾选要买的商品',
					  icon:'none'
					})
					return
				}
				
				let goodsData = [];
				list.forEach(item => {
					if (item.checked) {
						goodsData.push(item.id)
					}
				})
				let goodsDataStr = goodsData.join(',')
				uni.navigateTo({
					url: '/pages/order/index?cart='+goodsDataStr+'&type=cart'
				})
			}
		}
	}
</script>

<style>
	.container {
		min-height: 100vh;
		background-color: #f9f9f9;
	}

	.tuibtn {
		text-align: center;
		color: #fff;
		font-size: 28upx;
		background-color: #EC6104;
		display: inline-block;
		padding: 0 34upx;
		color: #fff;
		margin-left: 16upx;
	}

	.tuibtn::after {
		border: none;
	}

	.goods-info {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.empty {
		position: fixed;
		left: 0;
		top: 0;
		width: 100%;
		height: 100vh;
		padding-bottom: 100upx;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		background: #fff;

	}

	.empty image {
		width: 240upx;
		margin-bottom: 30upx;
	}

	.empty-tips {
		display: flex;
		font-size: 30upx;
		color: #666;

	}

	/* 购物车列表项 */
	.radio {
		position: absolute;
		top: 16upx;
		left: 20upx
	}

	uni-radio .uni-radio-input {
		width: 32upx;
		height: 32upx;
	}

	.image-wrapper {
		width: 160upx;
		height: 180upx;
		flex-shrink: 0;
		position: relative;
	}

	.image-wrapper image {
		width: 100%;
		height: 100%;
		border-radius: 6upx;
	}

	/* 底部栏 */
	.clear-btn {
		position: absolute;
		left: 26upx;
		top: 0;
		z-index: 4;
		width: 0;
		height: 52upx;
		line-height: 52upx;
		padding-left: 38upx;
		font-size: 26upx;
		color: #fff;
		background: #333;
		border-radius: 0 50px 50px 0;
		opacity: 0;
		transition: .2s;
	}

	.show {
		opacity: 1;
		width: 120upx;
	}

	.checkbox {
		height: 52upx;
		position: relative;


	}

	.checkbox image {
		width: 52upx;
		height: 100%;
		position: relative;
		z-index: 5;
	}

	.total-box {
		flex: 1;
		display: flex;
		flex-direction: column;
		text-align: right;
		padding-right: 40upx;

	}

	.action-section {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 95;
		display: flex;
		align-items: center;
		width: 100%;
		height: 94upx;
		background: #fff;
	}

	.confirm-btn {
		padding: 0 38upx;
		margin: 0;
		border-radius: 100px;
		height: 76upx;
		line-height: 76upx;
		font-size: 26upx;
		background: #1D84E8;
		box-shadow: 1px 2px 5px rgba(217, 60, 93, 0.72)
	}

	/* 复选框选中状态 */
	.action-section .checkbox.checked,
	.cart-item .checkbox.checked {
		color: #1D84E8;
	}
	.uni-tip {
			/* #ifndef APP-NVUE */
			display: flex;
			flex-direction: column;
			/* #endif */
			padding: 15px 0;
			width: 300px;
			background-color: #fff;
			border-radius: 10px;
		}
		.uni-tip-title {
			margin-bottom: 10px;
			text-align: center;
			font-weight: bold;
			font-size: 16px;
			color: #333;
		}
		.uni-tip-content {
			padding:20px 0;
			text-align: center;
			font-size: 16px;
			color: #666;
		}
		.uni-tip-group-button {
			/* #ifndef APP-NVUE */
			display: flex;
			/* #endif */
			flex-direction: row;
			padding-top:15px;
			border-top:1px solid #eee
		}
		.uni-tip-button {
			flex: 1;
			text-align: center;
			font-size: 14px;
			color: #3b4144;
		}
</style>
